package software.hust.com.hust_last.presenter;

import software.hust.com.hust_last.MyApplication;
import software.hust.com.hust_last.network.MyPackageData;
import software.hust.com.hust_last.network.MyPackageResponse;
import software.hust.com.hust_last.callback.ObjectCallback;
import software.hust.com.hust_last.util.AppConfig;
import software.hust.com.hust_last.util.LogUtil;
import software.hust.com.hust_last.util.ToastUtil;
import software.hust.com.hust_last.view.MyPackageAct;

/**
 * Created by zwjian on 2017/11/28.
 */

public class MyPackagePresenter extends BasePresenter<MyPackageAct> {
    public MyPackagePresenter(MyPackageAct view) {
        super(view);
    }

    private boolean hasMore = true;
    private int currentNum;

    private String userID = MyApplication.mApplication.loginUserInfo.user_id + "";
    private String token = MyApplication.mApplication.loginUserInfo.user_token;

    public void requestData(int position) {
        MyPackageData data = new MyPackageData();
        data.user_id = userID;
        data.user_token = token;
        data.position = position + "";
        data.request(this, new ObjectCallback<MyPackageResponse>() {
            @Override
            public void onSuccess(int responseCode, MyPackageResponse response) {
                currentNum = 0;
                if (AppConfig.NET_SUCCESS.equals(response.result_code) && getView() != null) {
                    currentNum += response.warehouse.size();
                    getView().setData(response.warehouse);
                } else if ("100".equals(response.result_code)) {
                    ToastUtil.show(getView(), "已无更多记录");
                    hasMore = false;
                } else if ("101".equals(response.result_code)) {
                    ToastUtil.show(getView(), "登录过期，请重新登录");
                }

            }

            @Override
            public void onFailure(String errMsg) {
                ToastUtil.show(getView(), "获取记录失败，请稍后重试");
                LogUtil.i("mypackageAct", "errMsg: " + errMsg);
            }
        });
    }

    public void requestMore(int position) {
        if (!hasMore) {
            return;
        }
        MyPackageData data = new MyPackageData();
        data.user_id = userID;
        data.user_token = token;
        data.position = position + "";
        data.request(this, new ObjectCallback<MyPackageResponse>() {
            @Override
            public void onSuccess(int responseCode, MyPackageResponse response) {
                if (AppConfig.NET_SUCCESS.equals(response.result_code) && getView() != null) {
                    if (response.warehouse != null && !response.warehouse.isEmpty()) {
                        getView().appendData(response.warehouse, hasMore);
                    }
                    currentNum += response.warehouse.size();
                } else if ("100".equals(response.result_code)) {
                    ToastUtil.show(getView(), "无更多记录");
                    hasMore = false;
                } else if ("101".equals(response.result_code)) {
                    ToastUtil.show(getView(), "登录过期，请重新登录");
                }
            }

            @Override
            public void onFailure(String errMsg) {
                ToastUtil.show(getView(), "获取记录失败，请稍后重试");
                LogUtil.i("MyTreasureAct", "errMsg: " + errMsg);
            }
        });
    }
}

